package com.example.leetcode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Stack;

/**
 * 括号有效性问题
 */
public class Demo0020 {

    public static void main(String[] args) {
        System.out.println(isValid("()()[[[]]]"));
    }
    public static boolean isValid(String str){
        int len = str.length();
        if (len/2==1){
            return false;
        }
        Map<Character, Character> map=new HashMap<>();
        map.put(')','(');
        map.put(']','[');
        map.put('}','{');
//        LinkedList<Character> stack = new LinkedList<>();
        Stack stack=new Stack<Character>();
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (map.containsKey(ch)){
                if (stack.isEmpty()||stack.peek()!=map.get(ch)) {
                    return false;
                }
                stack.pop();
            }else {
                stack.push(ch);
            }
        }

        return stack.isEmpty();
    }
}
